package com.yxq.mdt.app.modules.bottomsheet;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.yxq.mdt.app.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @author L
 * @time 2019-10-30
 * @describe 选择婚恋情况
 */
public class PersonalLoveSheetDialogFragment extends BottomSheetDialogFragment {
    Unbinder unbinder;
    @BindView(R.id.dialog_tv_save)
    TextView dialogTvSave;
    @BindView(R.id.dialog_check_box_single)
    CheckBox dialogCheckBoxSingle;
    @BindView(R.id.dialog_check_box_in_love)
    CheckBox dialogCheckBoxInLove;
    @BindView(R.id.dialog_check_box_married)
    CheckBox dialogCheckBoxMarried;
    @BindView(R.id.dialog_check_box_widowed)
    CheckBox dialogCheckBoxWidowed;
    @BindView(R.id.dialog_check_box_confidentiality)
    CheckBox dialogCheckBoxConfidentiality;
    String[] data = new String[]{"单身", "恋爱中", "已婚", "离异/丧偶"};
    String marriage;
    int marriageShow;
    @BindView(R.id.dialog_love_layout_single)
    RelativeLayout dialogLoveLayoutSingle;
    @BindView(R.id.dialog_love_layout_in_love)
    RelativeLayout dialogLoveLayoutInLove;
    @BindView(R.id.dialog_love_layout_married)
    RelativeLayout dialogLoveLayoutMarried;
    @BindView(R.id.dialog_love_layout_widowed)
    RelativeLayout dialogLoveLayoutWidowed;

    public PersonalLoveSheetDialogFragment(String marriage, int marriageShow) {
        this.marriage = marriage;
        this.marriageShow = marriageShow;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置背景透明，才能显示出layout中诸如圆角的布局，否则会有白色底（框）
        setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.personal_love_bottom_sheet_fragment_dialog, container, false);
        unbinder = ButterKnife.bind(this, view);
        initViews();
        return view;
    }

    private int selectPosition = -1;

    private void initViews() {
        dialogCheckBoxConfidentiality.setChecked(marriageShow == 1);
        if (marriage != null) {
            switch (marriage) {
                case "单身":
                    dialogCheckBoxSingle.setChecked(true);
                    break;
                case "恋爱中":
                    dialogCheckBoxInLove.setChecked(true);
                    break;
                case "已婚":
                    dialogCheckBoxMarried.setChecked(true);
                    break;
                case "离异/丧偶":
                    dialogCheckBoxWidowed.setChecked(true);
                    break;
            }
        }

        dialogCheckBoxSingle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                selectPosition = 0;
                if (!b) {
                    return;
                }
                dialogCheckBoxInLove.setChecked(!b);
                dialogCheckBoxMarried.setChecked(!b);
                dialogCheckBoxWidowed.setChecked(!b);
            }
        });
        dialogCheckBoxInLove.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                selectPosition = 1;
                if (!b) {
                    return;
                }
                dialogCheckBoxSingle.setChecked(!b);
                dialogCheckBoxMarried.setChecked(!b);
                dialogCheckBoxWidowed.setChecked(!b);
            }
        });
        dialogCheckBoxMarried.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                selectPosition = 2;
                if (!b) {
                    return;
                }
                dialogCheckBoxSingle.setChecked(!b);
                dialogCheckBoxInLove.setChecked(!b);
                dialogCheckBoxWidowed.setChecked(!b);
            }
        });
        dialogCheckBoxWidowed.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                selectPosition = 3;
                if (!b) {
                    return;
                }
                dialogCheckBoxSingle.setChecked(!b);
                dialogCheckBoxInLove.setChecked(!b);
                dialogCheckBoxMarried.setChecked(!b);
            }
        });
    }

    PersonalLoveSheetDialogFragmentInterface personalLoveSheetDialogFragmentInterface;

    public void setPersonalLoveSheetDialogFragmentInterface(PersonalLoveSheetDialogFragmentInterface personalLoveSheetDialogFragmentInterface) {
        this.personalLoveSheetDialogFragmentInterface = personalLoveSheetDialogFragmentInterface;
    }

    public interface PersonalLoveSheetDialogFragmentInterface {
        void checked(int index, String data, boolean confidentiality);
    }

    @OnClick(R.id.dialog_tv_save)
    public void onViewClicked() {
        dismiss();
        if (personalLoveSheetDialogFragmentInterface != null) {
            personalLoveSheetDialogFragmentInterface.checked(selectPosition, selectPosition != -1 ? data[selectPosition] : "", dialogCheckBoxConfidentiality.isChecked());
        }
    }
}
